<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" :width="1400">
    <el-form
      class="mb-10px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="74px"
    >
      <el-form-item label="零件名称" prop="partsName">
        <el-input
          v-model="queryParams.partsName"
          placeholder="请输入零件名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="加工工序" prop="processing">
        <el-input
          v-model="queryParams.processing"
          placeholder="请输入加工工序"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTableRef"
      v-loading="loading"
      :data="machiningData"
      border
      style="width: 100%"
      :max-height="450"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" align="center" type="index" width="80" />
      <el-table-column label="零件名称" align="center" prop="partsName" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="加工工序" align="center" prop="processing" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="单价 (元)" align="center" prop="unitPrice" />
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="loading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as WageProcessValuationApi from '@/api/config/wageProcessValuation'

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const machiningData = ref([]) // 机加数据
const queryParams = reactive({
  partsName: undefined,
  specification: undefined,
  processing: undefined
})
const queryFormRef = ref() // 搜索的表单
const multipleTableRef = ref()

/** 打开弹窗 */
const open = async (arr: []) => {
  dialogVisible.value = true
  // 修改时，设置数据
  loading.value = true
  try {
    await getList()
    nextTick(() => {
      arr.map((val) => {
        machiningData.value.map((item) => {
          if (val === item['id']) {
            multipleTableRef.value!.toggleRowSelection(item, true)
          }
        })
      })
    })
  } finally {
    loading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

// 获取数据
const getList = async () => {
  loading.value = true
  try {
    const data = await WageProcessValuationApi.getWageMachiningData(queryParams)
    machiningData.value = data
  } finally {
    loading.value = false
  }
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  loading.value = true
  try {
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', multipleSelection.value)
  } finally {
    loading.value = false
  }
}
// 多选
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
</script>
